package com.meals.system.api.admin;

import com.meals.system.api.admin.param.BatchIdParam;
import com.meals.system.config.annotation.TokenToAdminUser;
import com.meals.system.entity.AdminUserToken;
import com.meals.system.service.WhitesMealSellerService;
import com.meals.system.service.WhitesMealUserService;
import com.meals.system.util.PageQueryUtil;
import com.meals.system.util.Result;
import com.meals.system.util.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Description:
 * Parm:
 * return:
 * Author: Chen
 * Date: 2021/12/10
 */
@RestController
@Api(value = "v1", tags = "1-4.后台管理系统管理注册商家模块接口")
@RequestMapping("/admin-api/v1")
public class WhitesMealAdminRegisteredSellerAPI {
    private static final Logger logger = LoggerFactory.getLogger(WhitesMealAdminRegisteredUserAPI.class);

    @Resource
    private WhitesMealSellerService whitesMealSellerService;

    /**
     * 列表
     */
    @RequestMapping(value = "/sellers", method = RequestMethod.GET)
    @ApiOperation(value = "注册商家列表", notes = "注册商家列表")
    public Result list(@RequestParam(required = false) @ApiParam(value = "页码") Integer pageNumber,
                       @RequestParam(required = false) @ApiParam(value = "每页条数") Integer pageSize,
                       @RequestParam(required = false) @ApiParam(value = "用户状态") Integer lockStatus, @TokenToAdminUser AdminUserToken adminUser){
        logger.info("adminUser:{}", adminUser.toString());
        if (pageNumber == null || pageNumber < 1 || pageSize == null || pageSize < 10){
            return ResultGenerator.genFailResult("参数异常!");
        }
        Map params = new HashMap(8);
        params.put("page", pageNumber);
        params.put("limit",pageSize);
        if (lockStatus != null){
            params.put("orderStatus", lockStatus);
        }
        PageQueryUtil pageUtil = new PageQueryUtil(params);
        return ResultGenerator.genSuccessResult(whitesMealSellerService.getWhitesMealSellersPage(pageUtil));
    }

    /**
     * 用户禁用与解除禁用(0-未锁定 1-已锁定)
     */
    @RequestMapping(value = "/sellers/{lockStatus}", method = RequestMethod.PUT)
    @ApiOperation(value = "修改用户状态", notes = "批量修改，用户禁用与解除禁用(0-未锁定 1-已锁定)")
    public Result lockUser(@RequestBody BatchIdParam batchIdParam, @PathVariable int lockStatus, @TokenToAdminUser AdminUserToken adminUser){
        logger.info("adminUser:{}", adminUser.toString());
        if (batchIdParam==null||batchIdParam.getIds().length < 1){
            return ResultGenerator.genFailResult("参数异常!");
        }
        if (lockStatus != 0 && lockStatus != 1){
            return ResultGenerator.genFailResult("操作非法!");
        }
        if (whitesMealSellerService.lockSellers(batchIdParam.getIds(),lockStatus)){
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult("禁用失败");
        }
    }
}
